const {
    Mysql
} = require('../tools/utils');
module.exports = async (ctx) => {

    let {
        vid,
        uid,
        isLike
    } = ctx.query;
    if (vid != 'undefined' && uid != 'undefined' && isLike != 'undefined') {
        // 查询是否被收藏过
        let sql1 = `SELECT * from collect WHERE uid=${uid} AND vid=${vid}`;
        try {
            let res = await Mysql(sql1);
            // 如有则更新
            if (res.length >= 1) {
                try {
                    let sql2 = `UPDATE collect SET vid=${vid}, uid=${uid}, videoCover=(SELECT coverImg from video WHERE vid = ${vid}) , isLike=${isLike} ;`
                    await Mysql(sql2);
                    ctx.state.data = {
                        code: '001',
                        msg: 'ساقلىۋالدىڭىز'
                    }
                } catch (error) {
                    ctx.state.data = {
                        msg: 'مەغلۇپ بولدى',
                        code: '002'
                    }
                }
            } else {
                let sql3 = `INSERT INTO collect(vid,uid,videoCover,isLike) VALUES (${vid},${uid}, (SELECT coverImg from video WHERE vid = ${vid}),${isLike})`;
                try {
                    await Mysql(sql3);
                    ctx.state.data = {
                        code: '001',
                        msg: 'ساقلىۋالدىڭىز'
                    }
                } catch (error) {
                    ctx.state.data = {
                        msg: 'مەغلۇپ بولدى',
                        code: '002'
                    }
                }
            }
        } catch (error) {
            ctx.state.data = {
                msg: 'مەغلۇپ بولدى',
                code: '002'
            }
        }
    }


};